using System;
using System.Collections.Generic;
using System.Text;

namespace CMUSHrun.Database
{
    class Serializer
    {
        public enum SerializationFormat { Xml, Binary }

        public static System.Boolean SerializeToDisk(string path, object request, SerializationFormat format)
        {
            try
            {
                System.IO.MemoryStream oStream = Serialize(request, format);
                if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
                System.IO.FileStream output = System.IO.File.Open(path, System.IO.FileMode.CreateNew);
                output.Write(oStream.ToArray(), 0, Convert.ToInt32(oStream.Length));
                output.Close();
                oStream.Close();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static object DeSerializeFromDisk(string path, SerializationFormat format)
        {
            try
            {
                if (System.IO.File.Exists(path))
                {
                    System.IO.FileStream input = System.IO.File.Open(path, System.IO.FileMode.Open);
                    byte[] filecontents = new byte[Convert.ToInt32(input.Length)];
                    input.Read(filecontents, 0, Convert.ToInt32(input.Length));
                    input.Close();
                    //System.IO.File.Delete(path);
                    System.IO.MemoryStream inStream = new System.IO.MemoryStream(filecontents);
                    object newObj = DeSerialize(inStream, format);
                    inStream.Close();
                    return newObj;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        #region wrappers for binary and xml serialization
        public static System.IO.MemoryStream Serialize(object request, SerializationFormat format)
        {
            if (format == SerializationFormat.Binary)
                return SerializeBinary(request);
            else
                return SerializeSOAP(request);
        }
        public static object DeSerialize(System.IO.MemoryStream memStream, SerializationFormat format)
        {
            if (format == SerializationFormat.Binary)
                return DeSerializeBinary(memStream);
            else
                return DeSerializeSOAP(memStream);
        }
        #endregion

        #region Binary Serializers
        public static System.IO.MemoryStream SerializeBinary(object request)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            serializer.Serialize(memStream, request);
            return memStream;
        }

        public static object DeSerializeBinary(System.IO.MemoryStream memStream)
        {
            memStream.Position = 0;
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object newobj = deserializer.Deserialize(memStream);
            memStream.Close();
            return newobj;
        }
        #endregion

        #region XML Serializers

        public static System.IO.MemoryStream SerializeSOAP(object request)
        {
            System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            serializer.Serialize(memStream, request);
            return memStream;
        }

        public static object DeSerializeSOAP(System.IO.MemoryStream memStream)
        {
            object sr;
            System.Runtime.Serialization.Formatters.Soap.SoapFormatter deserializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
            memStream.Position = 0;
            sr = deserializer.Deserialize(memStream);
            memStream.Close();
            return sr;
        }
        #endregion

        #region miscel
        public static string ConvertStreamToString(System.IO.Stream theStream)
        {
            string theString = "";
            theStream.Position = 0;
            using (System.IO.StreamReader sr = new System.IO.StreamReader(theStream))
            {
                theString = sr.ReadToEnd();
                // Close and clean up the StreamReader
                sr.Close();
            }
            return theString;
        }
        #endregion
    }

}
